//
//  DataTool.m
//  DoubanDemo1
//
//  Created by lanou3g on 15/11/24.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DataTool.h"



@implementation DataTool
+ (void)requestDataWithURLStr:(NSString *)urlStr
                   HTTPMethod:(NSString *)method
                 withHttpBody:(NSString *)body
                   blockBlock:(BackBlock)backBlock
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    if ([method isEqualToString:@"POST"]) {
        [request setHTTPMethod:method];
        NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
    }
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (nil == error) {
            id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                backBlock(object);
            });
        }else{
            NSLog(@"%@",error);
        }
        
    }];
    [dataTask resume];
}


@end
